  function plusReady() {
  	// 弹出系统选择按钮框  
  	mui("body").on("tap", ".picBox", function() {
  		page.imgUp();
  	})

  }

  var page = null;
  var num = 0; //定义图片下标，初始化为0
  var index = 0; //数据图片下标
  var picArrary = new Array(); //定义一个图片数组，用来装图片路径
  var def = "file:///storage/emulated/0/Android/data/io.dcloud.HBuilder/apps/HBuilder/www/images/camera.jpg";
  page = {
  	imgUp: function() {
  		var m = this;
  		plus.nativeUI.actionSheet({
  			cancel: "取消",
  			buttons: [{
  					title: "拍照"
  				},
  				{
  					title: "从相册中选择"
  				}
  			]
  		}, function(e) { //1 是拍照  2 从相册中选择  
  			switch(e.index) {
  				case 1:
  					appendByCamera();
  					break;
  				case 2:
  					appendByGallery();
  					break;
  			}
  		});
  	}
  	//摄像头  
  }

  // 拍照添加文件
  function appendByCamera() {
  	plus.camera.getCamera().captureImage(function(e) {
  		console.log(e);
  		plus.io.resolveLocalFileSystemURL(e, function(entry) {
  			var path = entry.toLocalURL();
  			index = num % 3;
  			console.log(index);
  			var files = document.getElementById("headimg" + index); //获取选择的图片

  			num++;
  			console.log(files.src);
  			if(files.src == def && num <= 2) {
  				document.getElementById('pic_list').insertAdjacentHTML('beforeEnd', '<div class="picBox"><img id="headimg' + num + '" src="../images/camera.jpg"></div>');
  			}
  			files.src = path;
  			upload(files); //上传图片
  		}, function(e) {
  			mui.toast("读取拍照文件错误：" + e.message);
  		});

  	});
  }
  // 从相册添加文件
  function appendByGallery() {
  	plus.gallery.pick(function(path) {

  		index = num % 3;
  		console.log(index);
  		var files = document.getElementById("headimg" + index); //获取选择的图片

  		num++;
  		if(files.src == def && num <= 2) {
  			document.getElementById('pic_list').insertAdjacentHTML('beforeEnd', '<div class="picBox"><img id="headimg' + num + '" src="../images/camera.jpg"></div>');
  		}
  		files.src = path;
  		upload(files); //上传图片

  	});
  }

  //服务端接口路径
  var server = GOODS_API+"/upload.do";
  //获取图片元素
  //          var files = document.getElementById('headimg');
  // 上传文件
  function upload(files) {

  	var wt = plus.nativeUI.showWaiting();
  	var task = plus.uploader.createUpload(server, {
  			method: "POST"
  		},
  		function(t, status) { //上传完成
  			//			console.log("状态码："+t.responseText.resultCode);
  			var res = JSON.parse(t.responseText);
  			//			var def ="file:///storage/emulated/0/Android/data/io.dcloud.HBuilder/apps/HBuilder/www/images/camera.jpg";
  			if(res.resultCode == 0) { //上传成功后添加新图片
  				wt.close(); //关闭等待提示按钮
  				picArrary[index] = res.data;
  				plus.nativeUI.toast(res.msg);
  				console.log(JSON.stringify(picArrary));
  				
  			} else {
  				wt.close(); //关闭等待提示按钮
  				plus.nativeUI.toast(res.msg);
  			}
  		}
  	);
  	//添加其他参数
  	task.addData("title", "test");
  	//	console.log(files.src);
  	task.addFile(files.src, {
  		key: "file"
  	});
  	task.start();
  }
  if(window.plus) {
  	plusReady();
  } else {
  	document.addEventListener("plusready", plusReady, false);
  }